package aad.storyboard.view;

import java.util.ArrayList;

import utils.ViewUtils;


import aad.storyboard.model.Layer;
import aad.storyboard.model.Scene;
import aad.storyboard.model.Story;
import aad.storyboard.model.*;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.view.SurfaceView;
import android.widget.ImageView;

public class GraphicView extends ImageView{
        
        private Scene currentScene;        
        private ArrayList <GraphicalObject> graphicalObjects;       
        
        private ArrayList<Layer> layers;
        private Matrix idMatrix;        
                        
        public GraphicView(Context context, AttributeSet attrs){
        super(context, attrs);
        this.setBackgroundColor(Color.WHITE);
        this.idMatrix = new Matrix();
        ViewUtils.displayDensity = context.getResources().getDisplayMetrics().densityDpi;
        ViewUtils.screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        ViewUtils.screenHeight = context.getResources().getDisplayMetrics().heightPixels;
        
    }
        
        public GraphicView(Context context){
        super(context);   
        this.setBackgroundColor(Color.WHITE);     
        ViewUtils.displayDensity = context.getResources().getDisplayMetrics().densityDpi;
        ViewUtils.screenWidth = context.getResources().getDisplayMetrics().widthPixels;
        ViewUtils.screenHeight = context.getResources().getDisplayMetrics().heightPixels;               
        
    }
        /**
         * this is called when the mainview is invalidated by the framerate controller, redraws all the objects
         */
        @Override
        public void onDraw(Canvas canvas){
                canvas.setMatrix(idMatrix);
        currentScene = Story.getInstance().getCurrentChapter().getCurrentScene();
        //System.out.println(currentScene.getId());
        layers = currentScene.getLayerMap();        
        for(int i = layers.size()-1;i>-1;i--){
                graphicalObjects = layers.get(i).getGraphicalObjects(); 
                for (GraphicalObject g: graphicalObjects){ 
                		//System.out.println("Drawing something "+g.getPosX()+g.getPosY());
                         g.draw(canvas);       
                }
        }      
       
        try {
                        Thread.sleep(50);
                } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }            
}
     
                
        }
